package com.lyn.auxiliarytool.views.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

/**
 * 白板
 * @author Lyn 2022/12/5
 * @version 1.0.0
 */
public class WhiteboardView extends View {
    private Paint paint;
    public static final int BRUSH_MODE = 1;
    public static final int ERASER_MODE = 2;
    private final Stack<Path> pathStack = new Stack<>();
    private int mode;

    public WhiteboardView(Context context) {
        super(context);
        init();
    }

    public WhiteboardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public void init(){
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(10);
        paint.setColor(Color.GREEN);
        paint.setDither(true);
    }

    public void setMode(int mode){
        this.mode = mode;
        if (mode == BRUSH_MODE){
            paint.setAlpha(255);
            paint.setXfermode(null);
        }
        if (mode == ERASER_MODE){
            paint.setAlpha(0);
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRGB(255,255,255);
        if (pathStack.isEmpty()){
            return;
        }
        for (Path path:pathStack){
            canvas.drawPath(path,paint);
        }
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                pathStack.push(new Path()).moveTo(x,y);
                Log("down");
                break;
            case MotionEvent.ACTION_MOVE:
                pathStack.peek().lineTo(x,y);
                break;
            case MotionEvent.ACTION_UP:
                if (mode==ERASER_MODE){
                    Log("up");
                    if (pathStack.size()>1){
                        Path popPath = pathStack.pop();
                        Path peekPath = pathStack.peek();
                        boolean op = peekPath.op(popPath, peekPath, Path.Op.INTERSECT);
                        if (op){
                            pathStack.pop();
                        }
                        setMode(BRUSH_MODE);
                    }
                }
                break;
            default:
                Log(event.getAction()+"");
                break;
        }
        invalidate();
        return true;
    }

    public void Log(String msg){
        Log.e("WhiteboardView",msg);
    }
}
